package com.rabbitmq.delay.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangwu
 * @create2021/6/16
 */
@Configuration
public class RabbitConfig {

    //2个交换机、4个队列、4个路由
    //延迟交换机
    public static final String DELAY_EXCHANGE_NAME="delay.exchange";
    //延迟routingkeyA、B
    public static final String DELAY_QUEUE_A_ROUTINGKEY="delay.queue.a.routingkey";
    public static final String DELAY_QUEUE_B_ROUTINGKEY="delay.queue.b.routingkey";
    //延迟队列A、B
    public static final String DELAY_QUEUE_A="delay.queue.a";
    public static final String DELAY_QUEUE_B="delay.queue.b";


    //死信交换机
    public static final String DEAD_LETTER_EXCHANGE_NAME="dead.letter.exchange";
    //死信routingkeyA、B
    public static final String DEAD_LETTER_QUEUE_10S_ROUTINGKEY="dead.letter.queue_10s.routingkey";
    public static final String DEAD_LETTER_QUEUE_60S_ROUTINGKEY="dead.letter.queue_60s.routingkey";
    //死信队列A、B
    public static final String DEAD_LETTER_QUEUE_A="dead.letter.queue.a";
    public static final String DEAD_LETTER_QUEUE_B="dead.letter.queue.b";

    @Bean("delayExchange")
    public DirectExchange delayExchange(){
        return new DirectExchange(DELAY_EXCHANGE_NAME);
    }
    @Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange(){
        return new DirectExchange(DEAD_LETTER_EXCHANGE_NAME);
    }
    @Bean("delayQueueA")
    public Queue delayQueueA(){
        Map<String,Object> args=new HashMap<>();
        args.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE_NAME);
        args.put("x-dead-letter-routing-key",DEAD_LETTER_QUEUE_10S_ROUTINGKEY);
        args.put("x-message-ttl",10000);
        return QueueBuilder.durable(DELAY_QUEUE_A).withArguments(args).build();
    }
    @Bean("delayQueueB")
    public Queue delayQueueB(){
        Map<String,Object> args=new HashMap<>();
        args.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE_NAME);
        args.put("x-dead-letter-routing-key",DEAD_LETTER_QUEUE_60S_ROUTINGKEY);
        args.put("x-message-ttl",60000);
        return QueueBuilder.durable(DELAY_QUEUE_B).withArguments(args).build();
    }

    @Bean
    public Binding bindingDelaydQueueA(@Qualifier("delayQueueA")Queue delayQueueA,@Qualifier("delayExchange")DirectExchange delayExchange){
       return BindingBuilder.bind(delayQueueA).to(delayExchange).with(DELAY_QUEUE_A_ROUTINGKEY);
    }
    @Bean
    public Binding bindingDelayQueueB(@Qualifier("delayQueueB")Queue delayQueueB,@Qualifier("delayExchange")DirectExchange delayExchange){
        return BindingBuilder.bind(delayQueueB).to(delayExchange).with(DELAY_QUEUE_B_ROUTINGKEY);
    }

    @Bean("deadLetterQueueA")
    public Queue deadLetterQueueA(){
        return new Queue(DEAD_LETTER_QUEUE_A);
    }
    @Bean("deadLetterQueueB")
    public Queue deadLetterQueueB(){
        return new Queue(DEAD_LETTER_QUEUE_B);
    }

    @Bean
    public Binding bindingDeadLetterQueueA(@Qualifier("deadLetterQueueA")Queue queue,@Qualifier("deadLetterExchange")DirectExchange deadLetterExchange){
        return BindingBuilder.bind(queue).to(deadLetterExchange).with(DEAD_LETTER_QUEUE_10S_ROUTINGKEY);
    }
    @Bean
    public Binding bindingDLetterQueueB(@Qualifier("deadLetterQueueB")Queue queue,@Qualifier("deadLetterExchange")DirectExchange deadLetterExchange){
        return BindingBuilder.bind(queue).to(deadLetterExchange).with(DEAD_LETTER_QUEUE_60S_ROUTINGKEY);
    }
}
